package com.wpzf.ndhc.service.impl;

import com.wpzf.common.core.utils.ServletUtils;
import com.wpzf.common.security.service.TokenService;
import com.wpzf.ndhc.utils.TableUtil;
import com.wpzf.ndhc.mapper.WpzfNdhcAPPWyjzMapper;
import com.wpzf.ndhc.service.WpzfNdhcAPPWyjzService;
import com.wpzf.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class WpzfNdhcAPPWyjzServiceImpl implements WpzfNdhcAPPWyjzService {

    @Autowired
    private WpzfNdhcAPPWyjzMapper wpzfNdhcAPPWyjzMapper;

    @Autowired
    private TokenService tokenService;


    @Override
    public Map<String, Object> getsql(String key) {
        return wpzfNdhcAPPWyjzMapper.getsql(key);
    }

    @Override
    public List<Map<String, Object>> executeQuery(Map<String, Object> m) {
        return wpzfNdhcAPPWyjzMapper.executeQuery(m);
    }

    @Override
    public List<Map<String, Object>> queryDictionarylist(String key) {
        return wpzfNdhcAPPWyjzMapper.queryDictionarylist(key);
    }

    @Override
    public List<Map<String, Object>> getPageData(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.getPageData(param);
    }

    @Override
    public List<Map<String, Object>> getPch(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.getPch(param);
    }

    @Override
    public List<Map<String, Object>> getSheng(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.getSheng(param);
    }

    @Override
    public List<Map<String, Object>> getShi(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.getShi(param);
    }

    @Override
    public List<Map<String, Object>> getXian(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.getXian(param);
    }

    @Override
    public List<Map<String, Object>> getXz(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.getXz(param);
    }

    @Override
    public List<Map<String, Object>> getCun(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.getCun(param);
    }

    @Override
    public Integer getPageDataCount(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.getPageDataCount(param);
    }

    @Override
    public List<Map<String, Object>> getTaskDetails(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.getTaskDetails(param);
    }

    @Override
    public Integer getTaskDetailsCount(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.getTaskDetailsCount(param);
    }

    @Override
    public List<Map<String, Object>> queryAddDkList(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.queryAddDkList(param);
    }

    @Override
    public Integer queryAddDkListCount(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.queryAddDkListCount(param);
    }

    @Override
    public Integer addTaskDetails(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        Integer rest = 0;
        String task_id = param.get("task_id") + "";
        wpzfNdhcAPPWyjzMapper.updateTaskCount(param);

        Map<String, Object> task = wpzfNdhcAPPWyjzMapper.queryTaskById(param);
        if (task != null) {
            // 省

            // 市
            StringBuffer city = new StringBuffer(task.get("city") + "");
            List<String> shiList = (List<String>) param.get("shi");
            for (String s : shiList) {
                if (city.indexOf(s) == -1) {
                    city.append(",");
                    city.append(s);
                }
            }
            // 县
            StringBuffer xmc = new StringBuffer(task.get("xmc") + "");
            List<String> xmcList = (List<String>) param.get("xian");
            for (String s : xmcList) {
                if (xmc.indexOf(s) == -1) {
                    xmc.append(",");
                    xmc.append(s);
                }
            }
            StringBuffer xzqdm = new StringBuffer(task.get("xzqdm") + "");
            List<String> xzqdmList = (List<String>) param.get("xzqdm");
            for (String s : xzqdmList) {
                if (xzqdm.indexOf(s) == -1) {
                    xzqdm.append(",");
                    xzqdm.append(s);
                }
            }
            // 乡镇
            StringBuffer xzmc = new StringBuffer(task.get("xzmc") + "");
            List<String> xzmcList = (List<String>) param.get("xz");
            for (String s : xzmcList) {
                if (xzmc.indexOf(s) == -1) {
                    xzmc.append(",");
                    xzmc.append(s);
                }
            }
            // 村
            StringBuffer cmc = new StringBuffer(task.get("cmc") + "");
//            List<String> cmcList = (List<String>) param.get("cun");
            String cun = param.get("cun") + "";
            if (cmc.indexOf(cun) == -1) {
                cmc.append(",");
                cmc.append(cun);
            }

            Map<String, Object> m = new HashMap<>();
            m.put("task_id", task_id);
            m.put("city", city.toString());
            m.put("xmc", xmc.toString());
            m.put("xzqdm", xzqdm.toString());
            m.put("xzmc", xzmc.toString());
            m.put("cmc", cmc.toString());

            m.put("year",param.get("year"));
            m = TableUtil.TableName(m);
            wpzfNdhcAPPWyjzMapper.updateTask(m);
        }


        List<Map<String, Object>> taskdetailList = (List<Map<String, Object>>) param.get("taskdetail");

        for (Map<String, Object> map : taskdetailList) {
            map.put("task_id", task_id);
            map.put("taskdetail_id", UUID.randomUUID().toString());

            map.put("year",param.get("year"));
            map = TableUtil.TableName(map);
            rest += wpzfNdhcAPPWyjzMapper.addTaskdetail(map);
        }
        return rest;
    }

    @Override
    public Integer update(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.update(param);
    }

    @Override
    @Transactional
    public Integer del(Map<String, Object> param) {
        param = TableUtil.TableName(param);
//        wpzfNdhcAPPWyjzMapper.deleteTaskdetail(param);
//        wpzfNdhcAPPWyjzMapper.deleteTaskdetailAnnex(param);
        return wpzfNdhcAPPWyjzMapper.deleteTask(param);
    }

    @Override
    public List<Map<String, Object>> getDataNoAssigning(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.getDataNoAssigning(param);
    }

    @Override
    public Integer getDataNoAssigningCount(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.getDataNoAssigningCount(param);
    }

    @Override
    public List<Map<String, Object>> getAssignPersonnel(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        List<Map<String, Object>> rest = new ArrayList<>();
        String xzqdm = param.get("xzqdm") + "";
        if (xzqdm.length() == 2) {
            if ("省".equals(param.get("dj") + "")) {
                rest = wpzfNdhcAPPWyjzMapper.getAdministratorsSheng(param);
            } else if ("市".equals(param.get("dj") + "")) {
                rest = wpzfNdhcAPPWyjzMapper.getAdministratorsShi(param);
            } else if ("县".equals(param.get("dj") + "")) {
                rest = wpzfNdhcAPPWyjzMapper.getAdministratorsXian(param);
            }
        } else if (xzqdm.length() == 4) {
            if ("市".equals(param.get("dj") + "")) {
                rest = wpzfNdhcAPPWyjzMapper.getOperatorsShi(param);
            } else if ("县".equals(param.get("dj") + "")) {
                rest = wpzfNdhcAPPWyjzMapper.getAdministratorsXian(param);
            }
        } else if (xzqdm.length() == 6) {
            if ("县".equals(param.get("dj") + "")) {
                rest = wpzfNdhcAPPWyjzMapper.getOperatorsXian(param);
            }
        }
        return rest;
    }

    @Override
    public Integer addTask(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        Integer rest = 0;
        Map<String, Object> taskMap = (Map<String, Object>) param.get("task");
        taskMap.put("year",param.get("year"));
        taskMap = TableUtil.TableName(taskMap);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        taskMap.put("createid", loginUser.getSysUser().getUserId());
        taskMap.put("creator", loginUser.getSysUser().getUserName());
        taskMap.put("createdate", null);
        String xzqdm = loginUser.getSysUser().getXzqdm();
        if (xzqdm.length() == 2) {
            taskMap.put("creator_lever", "省");
        } else if (xzqdm.length() == 4) {
            taskMap.put("creator_lever", "市");
        } else if (xzqdm.length() == 6) {
            taskMap.put("creator_lever", "县");
        }

        List<Integer> userList = (List<Integer>) taskMap.get("userList");
        for (Integer ower_user_id : userList) {
            String task_id = UUID.randomUUID().toString();
            taskMap.put("task_id", task_id);
            taskMap.put("zpry", ower_user_id);
            Integer i = wpzfNdhcAPPWyjzMapper.addTask(taskMap);
            if (i > 0) {
                List<Map<String, Object>> taskdetailList = (List<Map<String, Object>>) param.get("taskdetail");
                for (Map<String, Object> map : taskdetailList) {
                    map.put("task_id", task_id);
                    map.put("taskdetail_id", UUID.randomUUID().toString());
                    map.put("year",param.get("year"));
                    map = TableUtil.TableName(map);
                    rest += wpzfNdhcAPPWyjzMapper.addTaskdetail(map);
                }
            }
        }
        return rest;
    }

    @Override
    @Transactional
    public Integer removeTask(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        Integer rest = 0;
        Map<String, Object> djzTaskdetail = wpzfNdhcAPPWyjzMapper.queryDjzTaskdetail(param);
        if (djzTaskdetail != null) {
            djzTaskdetail.put("year",param.get("year"));
            djzTaskdetail = TableUtil.TableName(djzTaskdetail);
            rest = wpzfNdhcAPPWyjzMapper.removeTaskdetail(param);
            wpzfNdhcAPPWyjzMapper.removeTaskdetailAnnex(djzTaskdetail);
        } else {
            rest = -1;
        }
        return rest;
    }

    @Override
    public List<Map<String, Object>> querySheng(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.querySheng(param);
    }

    @Override
    public List<Map<String, Object>> queryShi(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.queryShi(param);
    }

    @Override
    public List<Map<String, Object>> queryXian(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.queryXian(param);
    }

    @Override
    public List<Map<String, Object>> queryReviewUsers(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.queryReviewUsers(param);
    }

    @Override
    public Integer queryReviewUsersCount(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcAPPWyjzMapper.queryReviewUsersCount(param);
    }

    @Override
    public Integer register(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        Integer rest = 0;
        //查询用户名是否重复
        List<Map<String, Object>> user = wpzfNdhcAPPWyjzMapper.queryUserByUsername(param);
        if (user != null && user.size() > 0) {
            //用户名已存在
            return -1;
        } else {
            //保存到县级用户申请表
            param.put("submitdate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            param.put("password", passwordEncoder.encode(param.get("password") + ""));
            rest = wpzfNdhcAPPWyjzMapper.saveWyUser(param);
        }
        return rest;
    }


    @Override
    @Transactional
    public Integer reviewUsers(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        Integer rest = 0;
        List<Map<String, Object>> user = wpzfNdhcAPPWyjzMapper.queryUser(param);
        if (user != null && user.size() > 0) {
            //用户名已存在
            return -1;
        } else {
            param.put("sh_submitdate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
            rest = wpzfNdhcAPPWyjzMapper.updateWyXianUserState(param);
            if (rest > 0 && "1".equals(param.get("state") + "")) {
                //审核通过注册用户
                Integer user_id = wpzfNdhcAPPWyjzMapper.getUserId(param);
                param.put("user_id", user_id);
                wpzfNdhcAPPWyjzMapper.addUser(param);
                wpzfNdhcAPPWyjzMapper.deleteUserRoleByUserId(param);
                wpzfNdhcAPPWyjzMapper.deleteUserPlatformRoleByUserId(param);
                wpzfNdhcAPPWyjzMapper.addUserRole(param);
                wpzfNdhcAPPWyjzMapper.addUserPlatformRole(param);
            }
        }
        return rest;
    }


    @Override
    public String queryMsg(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        List<String> list = wpzfNdhcAPPWyjzMapper.queryMsg(param);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (i>0){
                sb.append("\n");
            }
            sb.append(list.get(i));
        }
        return sb.toString();
    }

}
